<?php
/*
 * function mycurl() @param url:A string containing a base qualified URI no
* argument method: a string is get or post arg: a array format url argument
* data��a array format post' argument retruntransfer: bool no or return result
*/
function mycurl($url, $method, $arg, $data = NULL, $returntransfer = true,$addtional_header='',$return_header=false) {
  $method = strtolower($method);
  $header = array();
  $rsstr = '?';
  foreach ($arg as $key => $val) {
    $rsstr .= $key . '=' . $val;
    $rsstr .= '&';
  }
  $rsstr = substr($rsstr, 0, -1);
  $url = $url . $rsstr;

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);

  if ($returntransfer) {
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  }
  if(strstr($url, 'https://') !== FALSE){
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

  }
  if ($method == 'post') {
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  }
  if($addtional_header){
    $header[] = $addtional_header;
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  }
  //curl_setopt($ch,CURLOPT_HEADER,1);
  $rsdata = curl_exec($ch);

  if($return_header){
    $getinfo = curl_getinfo($ch);
    $rs = array();
    $rs['header'] = $getinfo;
    $rs['body'] = $rsdata;
  }else{
    $rs = $rsdata;
  }
  curl_close($ch);
  unset($ch);
  
  return $rs;
}

function IF_country_list(){
    $data=webform_get_country_list();
    $return=array();
    foreach($data as $k=>$v){
        $return[$v['key']]=$v['value'];        
    }
    return $return;
}

function get_nid_by_country($country){
//    defined($country) && return $country;
    
}

function get_contact_by_region($region, $limit = NULL) {
  if(!db_table_exists('field_data_field_region')){
    return null;
  }
  global $language;
  $lang_wf = (isset($language->language)) ? $language->language : WEBFORM_DEFAULT_LANGUAGE;
  $select = db_select('node', 'n')->fields('n',array('nid'));
  $select->join('field_data_field_region', 'region', 'region.entity_id = n.nid');
  $select->join('field_data_field_name','name','name.entity_id = n.nid');
  $select->condition('region.field_region_value', $region)
          ->condition('type', 'key_contact')
          ->condition('status', 1)
          ->condition('n.language', $lang_wf)
          ->orderBy('name.field_name_value');
  ;
  if ($limit) {
    $select->range(0, $limit);
  }
  $nids = $select->execute()->fetchCol();
  return $nids;
}